'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports System.IO

''' <summary>
''' This ViewModel features identity creation.
''' </summary>
Public Class CreateIdentityViewModel
    Private _ImageSelector As ImageSelectorViewModel

    Public Sub New()
        _ImageSelector = New ImageSelectorViewModel(ApplicationService.Current.DataStorageDirectory)
    End Sub

    ''' <summary>
    ''' Gets the ImageSelectorViewModel which allows to pick an identity image.
    ''' </summary>
    Public ReadOnly Property ImageSelector() As ImageSelectorViewModel
        Get
            Return _ImageSelector
        End Get
    End Property

    ''' <summary>
    ''' Creates an Identity with the specified FullName, Uin, Password and the selected image. All four properties
    ''' are mandatory.
    ''' </summary>
    Public Sub CreateIdentity(ByVal fullname As String, ByVal uin As String, ByVal password As String)
        If String.IsNullOrEmpty(fullname) Then Throw New ArgumentNullException("fullname")
        If String.IsNullOrEmpty(uin) Then Throw New ArgumentNullException("uin")
        If String.IsNullOrEmpty(password) Then Throw New ArgumentNullException("password")

        Dim id As IdentityManager.Identity

        Dim avatarPath As String = System.IO.Path.Combine(ApplicationService.Current.DataStorageDirectory.FullName, String.Format("{0}.default.jpg", fullname))

        AvatarImageService.CreateAvatarImageFromFile(ImageSelector.SelectedImageFile, avatarPath)

        id = New IdentityManager.Identity(fullname)
        id.Description = uin
        id.ImageUrl = avatarPath
        id.SetAttribute(IdentityAttributes.UinAttribute, uin)
        id.SetAttribute(IdentityAttributes.PasswordAttribute, password)
        id.SetAttribute(IdentityAttributes.ImageOriginalFilePathAttribute, ImageSelector.SelectedImageFile)

        ApplicationService.Current.IdentityProvider.CreateIdentity(id)
    End Sub

    ''' <summary>
    ''' Navigates to the SignIn page.
    ''' </summary>
    Public Sub NavigateToSignInPage()
        Core.Kernel.Services.GetService(Of INavigationService).NavigateToSignInPage()
    End Sub
End Class

Public NotInheritable Class IdentityAttributes
    Private Shared _UinAttribute As New IdentityUinAttribute
    Private Shared _PasswordAttribute As New IdentityPasswordAttribute
    Private Shared _ImageOriginalFilePathAttribute As New IdentityImageOriginalFilePathAttribute

    Public Shared ReadOnly Property UinAttribute() As IdentityUinAttribute
        Get
            Return _UinAttribute
        End Get
    End Property

    Public Shared ReadOnly Property PasswordAttribute() As IdentityPasswordAttribute
        Get
            Return _PasswordAttribute
        End Get
    End Property

    Public Shared ReadOnly Property ImageOriginalFilePathAttribute() As IdentityImageOriginalFilePathAttribute
        Get
            Return _ImageOriginalFilePathAttribute
        End Get
    End Property
End Class

''' <summary>
''' Identity Attribute representing the Identity's UIN.
''' </summary>
Public Class IdentityUinAttribute
    Implements IdentityManager.IIdentityAttribute(Of String)

    Public ReadOnly Property AttributeName() As String Implements IdentityManager.IIdentityAttribute.AttributeName
        Get
            Return "Uin"
        End Get
    End Property
End Class

''' <summary>
''' Identity Attribute representing the Identity's password.
''' </summary>
Public Class IdentityPasswordAttribute
    Implements IdentityManager.IIdentityAttribute(Of String)

    Public ReadOnly Property AttributeName() As String Implements IdentityManager.IIdentityAttribute.AttributeName
        Get
            Return "Password"
        End Get
    End Property
End Class

''' <summary>
''' Identity Attribute representing the Identity's avatar origin.
''' </summary>
Public Class IdentityImageOriginalFilePathAttribute
    Implements IdentityManager.IIdentityAttribute(Of String)

    Public ReadOnly Property AttributeName() As String Implements IdentityManager.IIdentityAttribute.AttributeName
        Get
            Return "ImageOriginalFilePath"
        End Get
    End Property
End Class